import {login, getMenu} from '@/api/login.js'
const user = {
    state: {
        name: '',
        email: '',
        mobile: '',
        iconPath: '',
        rule: '',
        menuResource: null,
        pageElementResource: null,
        id: ''
    },
    mutations: {
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_EMAIL: (state, email) => {
            state.email = email
        },
        SET_MOBILE: (state, mobile) => {
            state.mobile = mobile
        },
        SET_ICONPATH: (state, iconPath) => {
            state.iconPath = iconPath
        },
        SET_RULE: (state, rule) => {
            state.rule = rule
        },
        SET_MENU: (state, menuResource) => {
            state.menuResource = menuResource
        },
        SET_ELEMENT: (state, pageElementResource) => {
            state.pageElementResource = pageElementResource
        },
        SET_ID: (state, id) => {
            state.id = id
        }
    },
    actions: {
        'login': ({commit}, userInfo) => {
            return new Promise((resolve, reject) => {
                login(userInfo.loginName, userInfo.password).then(response => {
                    const data = response.data
                    commit('SET_NAME', data.data.user.name)
                    commit('SET_EMAIL', data.data.user.email)
                    commit('SET_MOBILE', data.data.user.mobile)
                    commit('SET_ICONPATH', data.data.user.iconPath)
                    commit('SET_RULE', data.data.rule)
                    console.log('原始菜单接口', data.data.menuResource)
                    // commit('SET_MENU', data.data.menuResource)
                    commit('SET_ELEMENT', data.data.pageElementResource)
                    commit('SET_ID', data.data.user.id)
                    resolve(data)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        'get_menu': ({commit}) => {
            return new Promise((resolve, reject) => {
                getMenu().then(response => {
                    let r = response.data
                    console.log('get_menu', r)
                    if (r.code === 0) {
                        commit('SET_MENU', r.data)
                        resolve(r)
                    } else {
                        reject(r)
                    }
                })
            })
        }
    }
}
export default user
